package civicinity.ist.psu.edu.rssfeed;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class RSSFeedService extends Service {

	private AlarmManager mAlarmManager;
	private PendingIntent pendingIntent;
	
	@Override
	public void onCreate() {
		Log.i("K", "start service");
		setAlarmTimer();
	}
	
	public static final int NOTIFICATION_ID = 20110610;
	
	@Override
	public void onDestroy() {
		Log.i("K", "stop service");
		stopSelf();
		mAlarmManager.cancel(pendingIntent);
		super.onDestroy();
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	private void setAlarmTimer() {
		mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
		
		// alarm for the search.
		Intent intent = new Intent(this, RSSFeedServiceReceiver.class);
		pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
		
		// every 60 minute so far. (12 times a day)
		mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 60 * 60 * 1000, 60 * 60 * 1000, pendingIntent);
	}
}
